<?php
namespace app\common\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use think\Log;
use app\common\request\Curl;
use app\common\model\Cars;

class Company extends Command
{
    protected function configure()
    {
        $this->setName('company')->setDescription('update supplier_no');
    }

    protected function execute(Input $input, Output $output)
    {
         $sTime = microtime(true);
    	self::car();
        $eTime = microtime(true);
        $time = $eTime - $sTime;
    	$output->writeln('消耗时间'.$time.'秒');
    }
    
    public static function car(){
        Db::name('cars')->where('supplier_no','eq','')->field('car_id,car_unique_id')->chunk(50,function($cars){
            $data['car_unique_id_arr'] = array_column($cars,'car_unique_id');
            $curl = new Curl;
            $header = config('car_hear.header');
            $url = config('car_hear.url');
            $res = $curl->set('CURLOPT_HTTPHEADER',$header)->post($data)->url($url);
            if ($curl->error()) {
                Log::record($curl->message(),'error');exit();
            }else{
                    // 任务进程信息
                $info = $curl->info();               
                if($info['http_code'] == 500){ 
                    Log::record('对方服务器错误','error');  
                    exit();
                }else{
                    // 任务结果内容
                    $content = $curl->data();
                }
            }
            $arr = json_decode($content,true);
            if($arr['result'] == 0){
                if(!empty($arr['data'])){
                    foreach ($arr['data'] as $key => $value) {
                        if($value['machine_report_time'] !==0){
                            
                            if($value['supplier_no'] == 1376920001){

                                Db::name('cars')->where('car_unique_id',$key)->update(['supplier_no'=>$value['supplier_no']]);
                            }
                        }

                    }
                }    
            }
        });
    }
} 